using JetBrains.Annotations;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using static Pathfinding.Funnel;

public class BagItem : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler,IPointerEnterHandler, IPointerExitHandler
{
    //记录当前的格子ID
    public int grild_id;

    Transform org;//源
    public void Awake()
    {

        grild_id = int.Parse(transform.gameObject.name.Split("_")[1]);

        //Debug.Log($"grild_id:__{grild_id},,name:__{transform.gameObject.name}");
    }

    GameObject temp;
    public void OnBeginDrag(PointerEventData eventData)
    {
        //复制一个 拖拽时 一样的 图标+物品
        //transform.gameObject.name.Split("_");

        if(transform.childCount > 0)
        {
            var p = transform.GetChild(0);
            p.gameObject.SetActive(false );
            org = p;
            int id = int.Parse(p.name);
            int count = int.Parse(p.transform.Find("key/key_text").GetComponent<Text>().text);
            temp =  ResourceManager.Instance.CreatePropItem(id,count);
            var rt = temp.GetComponent<RectTransform>();
            temp.transform.SetParent(BagViewController.Instance.view.transform,false);
            rt.anchorMin =  new Vector2 (0.5f,0.5f);
            rt.anchorMax = new Vector2(0.5f, 0.5f);
            rt.sizeDelta = new Vector2(120, 120f);
        }


    }
    public void OnDrag(PointerEventData eventData)
    {
        //更新这个物体的位置
        if(temp!=null)
        {
            temp.transform.position = eventData.position;  
        }
    }

    //结束拖拽
    public void OnEndDrag(PointerEventData eventData)
    {
        //判断 是叠加物品还是 交换位置 所交换的物品 有可能是孔
        if(temp!=null)
        {
            temp.gameObject.SetActive(false);
            ResourceManager.Instance.Destory(temp);
            temp = null;


            //拖去被select的物品 会消失
            int targetType,orgType;

            //Debug.Log($"org_name:__{org.parent.gameObject.name},,target_name:__{eventData.pointerEnter.gameObject.name}");

            //这里的Select 的层级会挡住 真正所需要的 propItem 咋搞呢？
            var target_name = eventData.pointerEnter.gameObject.name; //检查一下          

            if (target_name.StartsWith("Prop_"))
            {
                targetType = 1;
            }
            else if (target_name.StartsWith("Item_"))
            {
                targetType = 2;
            }
            else
            {
                return;
            }
            var tarGrild_Id = int.Parse(target_name.Split("_")[1]);

            var org_name = org.parent.gameObject.name;
            if (org_name.StartsWith("Prop_"))
            {
                orgType = 1;
            }
            else if (org_name.StartsWith("Item_"))
            {
                orgType = 2;
            }
            else
            {
                return;
            }
            var orgGrild_Id = int.Parse(org_name.Split("_")[1]);
            //Debug.Log($"grild_id:__{orgGrild_Id},,name:__{org_name}");



            if (eventData.pointerEnter.gameObject.CompareTag("Equip"))
            {
                BagEntity bagEntity = BagData.Instance.Get(orgGrild_Id);
                if (target_name.StartsWith("Item_"))
                {
                    if (bagEntity != null)
                    {
                        if (bagEntity.propConfig.part == tarGrild_Id) //部位相同
                        {
                            BagData.Instance.Switch_Equip(bagEntity); //数据逻辑 数据交换到装备上
                          
                            if (eventData.pointerEnter.gameObject.transform.childCount == 0) //空格子的情况
                            {
                                    
                            }
                            else
                            {
                                //交换逻辑
                                var c = eventData.pointerEnter.gameObject.transform.GetChild(0);
                                c.SetParent(transform, false);
                            }

                            org.transform.SetParent(eventData.pointerEnter.gameObject.transform, false);
                            org.gameObject.SetActive(true);
                        }
                        else
                        {
                            org.gameObject.SetActive(true);
                            return;
                        }
                    }
                    else
                    {
                        Debug.Log("Org is Null!!!!");
                    }


                }
            } // 拖进装备栏
            else //拖到了背包里面 
            {
                BagEntity bagEntity = BagData.Instance.Get(tarGrild_Id);
                if (org_name.StartsWith("Prop_"))
                {
                    BagEntity orgEntity = BagData.Instance.Get(orgGrild_Id);
                    BagData.Instance.Modify_Grild(orgGrild_Id, tarGrild_Id, null);
                    if (eventData.pointerEnter.gameObject.transform.childCount == 0) //空的背包格子 直接换过去 
                    {
                        org.transform.SetParent(eventData.pointerEnter.gameObject.transform, false);
                        org.gameObject.SetActive(true);
                        org.transform.localPosition = Vector3.zero; 
                    }
                    else
                    {
                        org.transform.SetParent(eventData.pointerEnter.gameObject.transform, false); //非空格子 要交换逻辑
                        org.gameObject.SetActive(true);
                        var c = eventData.pointerEnter.gameObject.transform.GetChild(0);
                        c.SetParent(transform, false);
                    }
                }
                else if (org_name.StartsWith("Item_"))
                {
                    BagEntity orgEntity = BagData.Instance.Get_Equip(orgGrild_Id);
                    //1、背包的格子为空  ->交换 逻辑
                    //2、end不是装备 || 部位不一致  ->直接add进去 卸下逻辑 
                    //3、end是装备 而且 部位一致 装备 交换 装备逻辑
                    BagData.Instance.EquipSwitch(orgGrild_Id, tarGrild_Id);
                    if (eventData.pointerEnter.gameObject.transform.childCount == 0) //空的背包格子 直接换过去 
                    {
                        org.transform.SetParent(eventData.pointerEnter.gameObject.transform, false);
                        org.gameObject.SetActive(true);
                        org.transform.localPosition = Vector3.zero;
                    }
                    else 
                    {
                        org.transform.SetParent(eventData.pointerEnter.gameObject.transform, false); //非空格子 要交换逻辑
                        org.gameObject.SetActive(true);
                        if (orgEntity.propConfig.part == bagEntity.propConfig.part)
                        {
                            var c = eventData.pointerEnter.gameObject.transform.GetChild(0);
                            c.SetParent(transform, false);
                        }
                        else
                        {
                            BagViewController.Instance.view.Show();
                        }

                    }

                    

                }
                else
                {
                    org.gameObject.SetActive(true);
                }
                org = null;
            }

        }
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        //这里 锚点不太对 应该是在左上角 

        //Debug.Log(eventData.pointerEnter.gameObject.name);

        if (!eventData.pointerEnter.gameObject.name.StartsWith("Prop")) return;
        if (eventData.pointerEnter.transform.childCount == 0) return;
        int grild_id = int.Parse(eventData.pointerEnter.gameObject.name.Split("_")[1]);
        BagViewController.Instance.view.SetPropInfo(eventData.pointerEnter.transform.position,grild_id, true);
        
        BagViewController.Instance.view.SelectObj = eventData.pointerEnter.gameObject;
        
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        BagViewController.Instance.view.SetPropInfo(Vector3.zero,isShow:false);
        BagViewController.Instance.view.SelectObj = null;
    }
}